<?php

namespace app\touch_screen\controller;

use app\common\business\Department;
use app\common\business\User;
use app\common\lib\Arr;
use app\common\lib\Param;
use app\common\lib\Show;
use app\common\lib\Str;
use app\Request;
use think\facade\Cache;

class Work extends ApiBase
{
    //获取零活列表
    public function publicList(){
        $page = input("page", 0, "intval");
        $limit = input("limit", 0, "intval");
        $type = input("type", 0, "intval");

        //筛选条件
        $where = [
            ['is_show','=',1], //公开
            //['process_status','=',1], //招工中
            //['start_time','>',time()] //开工日期大于当前时间
        ];

        //根据pid查字公众分类
        if(!empty($type)){
            $workTypeBis = new \app\common\business\WorkType();
            $mapWorkType = [
                ['pid','=',$type]
            ];
            $workTypeList = $workTypeBis->getListByWhere($mapWorkType,0,0,'id');
            if(empty($workTypeList)){
                $where[]=['type','=',$type];
            }else{
                $where[] = ['type','in',array_column($workTypeList,'id')];
            }
        }

        $workBis = new \app\common\business\Work();
        $list = $workBis->getListByWhere($where,$page,$limit,'id,department_id,name,address,start_time,price,valuation');
        $total = $workBis->getCountByWhere($where,'id');
        if (!empty($list)) {
            $list = $this->resetTempInfo($list);
        }
        return Show::success(['list'=>$list,'count'=>$total]);
    }

    //获取零活详情
    public function publicDetail()
    {
        $id = input("id", 0, "intval");

        $workBis = new \app\common\business\Work();
        //筛选条件
        $where = [
            ['id','=',$id],
            ['is_show','=',1], //公开
            //['process_status','=',1], //招工中
            //['start_time','>',time()] //开工日期大于当前时间 若增加此条件则无法查看开工后的详情
        ];
        $info = $workBis->getNormalByWhere($where);
        if (!empty($info)){
            $info = $workBis->resetTextList([$info]);
            $info = $info[0];
        }
        return Show::success($info);
    }

    private function resetTempInfo($list){
        if (empty($list)){
            return [];
        }

        //部门字典
        $departmentBis = new Department();
        $departmentIdArr = array_column($list,'department_id');
        if (!empty($departmentIdArr)){
            $departmentList = $departmentBis->getListByWhere([['id','in',$departmentIdArr]],0,0,'id,name,logo,employer_score,workers_score');
            $departmentArr = Arr::getKeyValueByTwoArray($departmentList,'','id',2);
        }
        $workBis = new \app\common\business\Work();
        $workConfig = config('work.valuation');

        foreach ($list as $k=>$v){
            $list[$k]['unit_text'] = $workBis->getUnitText($v['valuation']);
            $list[$k]['valuation_text'] = $workConfig[$v['valuation']] ?? '';
            $list[$k]['start_time'] = date('Y-m-d H:i:s',$v['start_time']);
            $list[$k]['department_arr'] = $departmentArr[$v['department_id']] ?? [];
        }
        return $list;
    }
}
